package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.domain.vo.VisitorVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserLikeCountService;
import com.itheima.service.mongo.VisitorService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserLikeManager {
    @DubboReference
    private UserLikeCountService userLikeCountService;

    @DubboReference
    private UserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @DubboReference
    private VisitorService visitorService;
    public ResponseEntity counts() {
        Long userId = UserHolder.getUserId();
        int eachLoveCount = userLikeCountService.eachLikeCount(userId);
        int loveCount = userLikeCountService.loveCount(userId);
        int fanCount = userLikeCountService.fanCount(userId);

        Map<String,Integer> map = new HashMap<>();
        map.put("eachLoveCount",eachLoveCount);
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);

        System.out.println(map);
        return ResponseEntity.ok(map);
    }

    public PageBeanVo queryLikeList(Integer page, Integer pageSize,Integer type) {
        Long userId = UserHolder.getUserId();
        List<UserLikeVo> userLikeVoList = new ArrayList<>();
        switch (type){
            case 1:{
                PageBeanVo pageBeanVo = userLikeCountService.eachLikeList(userId,page,pageSize);
                List<UserLike> userLikeList = (List<UserLike>) pageBeanVo.getItems();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        UserLikeVo userLikeVo = new UserLikeVo();
                        UserInfo userInfo = userInfoService.findByUserId(userLike.getUserId());
                        BeanUtil.copyProperties(userInfo,userLikeVo);
                        userLikeVoList.add(userLikeVo);
                    }
                }
                pageBeanVo.setItems(userLikeVoList);
                return pageBeanVo;
            }
            case 2:{
                PageBeanVo pageBeanVo = userLikeCountService.loveCountList(userId,page,pageSize);
                List<UserLike> userLikeList = (List<UserLike>) pageBeanVo.getItems();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        UserLikeVo userLikeVo = new UserLikeVo();
                        UserInfo userInfo = userInfoService.findByUserId(userLike.getLikeUserId());
                        BeanUtil.copyProperties(userInfo,userLikeVo);
                        userLikeVoList.add(userLikeVo);
                    }
                }
                pageBeanVo.setItems(userLikeVoList);
                return pageBeanVo;
            }
            case 3:{
                PageBeanVo pageBeanVo = userLikeCountService.fanList(userId, page, pageSize);
                List<UserLike> userLikeList = (List<UserLike>) pageBeanVo.getItems();

                if(CollectionUtil.isNotEmpty(userLikeList)){
                    for (UserLike userLike : userLikeList) {
                        UserLikeVo userLikeVo = new UserLikeVo();

                        UserInfo userInfo = userInfoService.findByUserId(userLike.getUserId());

                        BeanUtil.copyProperties(userInfo,userLikeVo);

                        userLikeVo.setAlreadyLove(userLikeCountService.common(userId,userLike.getUserId()));

                        userLikeVoList.add(userLikeVo);
                    }
                }
                pageBeanVo.setItems(userLikeVoList);
                return pageBeanVo;
            }
            case 4:{
                PageBeanVo visitorBeanVo = visitorService.findVisitor(userId,page,pageSize);
                List<Visitor> visitorList = (List<Visitor>) visitorBeanVo.getItems();

                List<UserLikeVo> userLikeVos = new ArrayList<>();

                if (CollectionUtil.isNotEmpty(visitorList)){
                    for (Visitor visitor : visitorList) {
                        //根据id查询userInfo数据
                        UserInfo userInfo = userInfoService.findByUserId(visitor.getVisitorUserId());
                        UserLikeVo vo = new UserLikeVo();

                        vo.setMatchRate(visitor.getScore().intValue());
                        BeanUtil.copyProperties(userInfo,vo);

                        userLikeVos.add(vo);
                    }
                }
                //更新时间
                stringRedisTemplate.opsForValue().set(ConstantUtil.LAST_ACCESS_TIME+userId,System.currentTimeMillis()+"");

                visitorBeanVo.setItems(userLikeVos);
                return visitorBeanVo;
            }
        }
        return null;
    }

    public void fans(String uid) {
        //向userLike表中添加一条数据
        UserLike userLike = new UserLike();
        userLike.setUserId(UserHolder.getUserId());
        userLike.setLikeUserId(Long.valueOf(uid));
        userLike.setCreated(System.currentTimeMillis());

        userLikeCountService.addUserLike(userLike);

        //userLikeService.common(UserHolder.getUserId(),Long.valueOf(uid));
    }

    //喜欢 取消
    public void deleteLike(String uid) {
        //根据我的id和我喜欢用户的id删除数据
        userLikeCountService.deleteCards(UserHolder.getUserId(),Long.valueOf(uid));
    }
}
